package com.wondream.myframework.thread;

import java.util.concurrent.*;

/**
 * 第四种创建线程的方式-使用自定义线程池new ThreadPoolExecutor
 * ThreadPoolExecutor(int corePoolSize, //核心线程数
 *                    int maximumPoolSize, //最大线程数
 *                    long keepAliveTime, //非核心线程数空闲等待时间
 *                    TimeUnit unit, //非核心线程数空闲等待时间单位
 *                    BlockingQueue<Runnable> workQueue, //等待队列
 *                    ThreadFactory threadFactory, //创建线程的工厂
 *                    RejectedExecutionHandler handler) //线程数超过最大线程数+阻塞队列大小后执行的拒绝策略
 *
 */
public class ThreadTest9 {
    public static void main(String[] args) throws Exception {
        // 1.自定义线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,10,2,TimeUnit.SECONDS,
                new SynchronousQueue<>(), Executors.defaultThreadFactory());
        for (int i = 0; i < 12; i++) {
            threadPool.submit(()->{
                try {
                    System.out.println("activeCount:"+threadPool.getActiveCount());
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        threadPool.shutdown();
    }

}
